/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.daten.struktur.liste;

import de.mnit.basis.daten.struktur.A_LM_Basis;
import de.mnit.basis.daten.struktur.S_Folge;
import de.mnit.basis.fehler.Fehler;

/**
 * @author Michael Nitsche
 */
public class Liste<T> extends A_LM_Basis<T> implements S_Liste<T> {

	public static <TT> Liste<TT> neu() {
		return new Liste<TT>();
	}

	public static <TT> Liste<TT> neu(Class<TT> c) {
		return new Liste<TT>();
	}

	public static <TT> Liste<TT> neu(TT... objekte) {
		Liste<TT> erg = neu();
		erg.plusAlle(objekte);
		return erg;
	}

	public static <TT> Liste<TT> neu(S_Folge<TT> objekte) {
		Liste<TT> erg = neu();
		erg.plusAlle(objekte);
		return erg;
	}

	// LESEN

	public int[] gPositionen(T objekt) { //TODO Umbauen auf indexOf!
		S_Liste<Integer> puffer=Liste.neu();
		for(int pos=0; pos<daten.size(); pos++) if(objekt.equals(daten.get(pos))) puffer.plus(pos+1);
		int[] erg=new int[puffer.gLaenge()];
		for(int i=1; i<=puffer.gLaenge(); i++) erg[i-1]=puffer.gib(i);
		return erg;
	}

	public Integer gPositionEins(T objekt) {
		int erg=daten.indexOf(objekt, 0);
		return erg<0 ? null : erg+1;
	}

	public S_Liste<T> kopie() {
		S_Liste<T> erg=Liste.neu();
		erg.plusAlle(this);
		return erg;
	}

	// ÄNDERN

	public void plus(T o) {
		daten.add(o);
	}

	public void plus(int pos, T o) {
		Fehler.zahl.wenn_Ausserhalb(1, daten.size()+1, pos);
		daten.add(pos-1, o);
	}

	public S_Liste<Integer> minus(T objekt) {
		S_Liste<Integer> puffer=Liste.neu();
		for(int pos=daten.size(); pos>0; pos--)
			if(gib(pos).equals(objekt)) {
				minus(pos);
				puffer.plus(1, pos);
			}
		return puffer;
	}

	public S_Liste<Integer> ersetzen(T objekt, T neu) {
		S_Liste<Integer> puffer=Liste.neu();
		for(int pos=1; pos<=daten.size(); pos++) {
			if(gib(pos).equals(objekt)) {
				setze(pos,neu);
				puffer.plus(pos);
			}
		}
		return puffer;
	}

}
